# -*- coding: utf-8 -*- 
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/9/27
# @File : 15.G - Longest Path.py


# def re(): #多源最短路径做法，TLE!!! 没考虑到DAG可以求以每个顶点为终点的最长简单路径。
#     ans = 0
#     for k in range(N):
#         for s in range(N):
#             for t in range(N):
#                 dp[s][t] = max(dp[s][t], dp[s][k] + dp[k][t])
#                 if(dp[s][t]>ans):
#                     ans = dp[s][t]
#     return ans


from collections import deque
def re_dag_solve():
    Q = deque([i for i in range(0, N) if indegree[i] == 0])  # 入度为0的入队
    dp = [0] * N # 计算以每个顶点为终点的最长path
    while Q:
        v = Q.popleft()
        for u in table[v]:
            if dp[v]+1 > dp[u]:
                dp[u] = dp[v]+1
            indegree[u] -= 1
            if indegree[u] == 0:  # 新的变成了入度为0的入队
                Q.append(u)
    return max(dp)

"""
题意:给你一张DAG,求图中的最长路径.

题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可.
    DAG可以求以每个顶点为终点的最长简单路径。

"""
if __name__ == '__main__':
    N, M = map(int, input().split())

    table = [list() for i in range(N)]
    indegree = [0] * (N)

    for _ in range(M):
        x,y = map(int, input().split())
        table[x-1].append(y-1)
        indegree[y-1] += 1

    res = re_dag_solve()
    print(res)